package com.uvlab.sitespot;

import com.uvlab.sitespot.init.JxAppBootScanner;
import com.uvlab.sitespot.init.JxStartupRunner;
import com.uvlab.sitespot.service.JxAppService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;

/**
 * Main Application.
 */
/*
 * @modified by zhangwei in 2016.05
 * - Replace annotation @SpringBootApplication with several independent annotations.
 *   Because we want to configure the packages to be scanned.
 * <p>
 * @modified by zhangwei on 2016.06.10
 * - Update method svc() and merged it with method getAppService().
 */
//@SpringBootApplication
@Configuration
@EnableAutoConfiguration
@EnableWebMvc
@ComponentScan
//@ComponentScan(basePackages={"com.uvlab.sitespot.init","com.uvlab.sitespot.io","com.uvlab.sitespot.service"})
public class JxApplication {

    JxAppBootScanner scanner = null;

    // @attention
    // svc must be managed by spring. you should NOT create it yourself. So I add
    // the method svc(). Attention "svc" is also the bean id.
    //
    // private JxAppService svc=new JxAppService();
    //
    // modified by zhangwei on 2016.06.17
    // You shouldnot add @Resource here because this variable is assigned
    // by the svc() method in this class other than managed by Spring Context.
    //
    // @Resource(name = "svc")
    JxAppService svc = null;

    public static void main(String[] args) throws Exception {
        SpringApplication.run(new Object[]{JxApplication.class}, args);
    }

    @PostConstruct
    public void init(){
        System.out.println("JxApplication.init() ...");
        assert(svc != null);
    }
    @PreDestroy
    public void  dostory(){
        System.out.println("JxApplication cleanup...");
    }

    @Bean
    public JxAppService svc()
    {
        if (svc == null) {
            if (scanner == null)
            {
                System.out.println("JxApplication.svc(): creating the JxAppBootScanner bean.");
                scanner = new JxAppBootScanner();
            }
            System.out.println("JxApplication.svc(): creating the JxAppService bean.");
            this.svc = new JxAppService();
            svc.init(this.scanner);
        }
        return svc;
    }

    /**
     * Cross Origin Resource Support(CORS) for the Spring MVC.
     *
     * @return The bean named "webMvcConfigurer". It's used by Spring MVC
     * automatically.
     */

    // Reference
    // Enabling Cross Origin Requests for a RESTful Web Service,
    // http://spring.io/guides/gs/rest-service-cors/
    //
    // CORS Support,
    // http://docs.spring.io/spring/docs/current/spring-framework-reference/html/cors.html
    //
    // Spring Boot CORS filter - CORS preflight channel did not succeed, 2016,
    // http://stackoverflow.com/questions/36809528/spring-boot-cors-filter-cors-preflight-channel-did-not-succeed
    //
    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurerAdapter() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                // registry.addMapping("/rest").allowedOrigins("http://localhost:9000");
                // registry.addMapping("/**").allowedOrigins("http://localhost");
                registry.addMapping("/**");
            }
        };
    }

    @Bean
    @Order(1)
    public JxStartupRunner schedulerRunner() {
        return new JxStartupRunner();
    }

    /*
    @Bean
    @Order(2)
    CommandLineRunner init() {
        return (String[] args) -> {
            new File(JxAppFoundation.DataRoot).mkdir();
        };
    }
    */
}